﻿using UnityEngine;
using LuaInterface;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class UIEventEx
{
    /// <summary>
    /// 添加注册按钮的注册事件
    /// </summary>
    /// <param name="gameObject"></param>
    /// <param name="luaFunction"></param>
    public static void AddButtonClick(GameObject gameObject,LuaFunction luaFunction)
    {
        if(gameObject == null || luaFunction == null) return;

        Button button = gameObject.GetComponent<Button>();
        if(button == null) return;

        button.onClick.AddListener(delegate()
        {
            luaFunction.Call(gameObject);
        });
    }

    /// <summary>
    /// 添加按钮的注册事件 参数
    /// </summary>
    /// <param name="gameObject"></param>
    /// <param name="luaFunction"></param>
    /// <param name="luaTable"></param>
    public static void AddButtonClick(GameObject gameObject,LuaFunction luaFunction,LuaTable luaTable)
    {
        if(gameObject == null || luaFunction == null || luaTable == null) return;

        Button button = gameObject.GetComponent<Button>();
        if(button == null) return;

        button.onClick.AddListener(delegate()
        {
            luaFunction.Call(gameObject, luaTable);
        });
    }

    /// <summary>
    /// 添加完成编辑的注册事件
    /// </summary>
    /// <param name="gameObject"></param>
    /// <param name="luaFunction"></param>
    public static void AddInputFieldEndEditHandle(GameObject gameObject,LuaFunction luaFunction)
    {
        if(gameObject == null || luaFunction == null) return;

        InputField inputField = gameObject.GetComponent<InputField>();
        if(inputField == null) return;

        inputField.onEndEdit.AddListener(delegate(string text)
        {
            luaFunction.Call(text);
        });
    }

    /// <summary>
    /// 注册点击事件的逻辑，在EventsSupport中激活
    /// </summary>
    /// <param name="gameObject"></param>
    /// <param name="luaFunction1"></param>
    /// <param name="luaFunction2"></param>
    public static void AddPointerDownUpSupport(GameObject gameObject,LuaFunction luaFunction1, LuaFunction luaFunction2)
    {
        if(gameObject == null || luaFunction1 == null || luaFunction2 == null) return;
        EventsSupport eventsSupport = gameObject.AddComponent<EventsSupport>();

        eventsSupport.InitDownUpHandler((PointerEventData pointerEventData) => 
        {
          if(luaFunction1 != null)
            luaFunction1.Call(gameObject, pointerEventData);  
        },(PointerEventData pointerEventData) => 
        {
            if(luaFunction2 != null)
                luaFunction2.Call(gameObject, pointerEventData);
        });
    }

    /// <summary>
    /// 给Toggle添加事件监听
    /// </summary>
    /// <param name="gameObject"></param>
    /// <param name="luaFunction"></param>
    public static void AddToggle(GameObject gameObject,LuaFunction luaFunction)
    {
        if(gameObject == null || luaFunction == null) return;
        Toggle toggle = gameObject.GetComponent<Toggle>();
        if(toggle == null) return;

        toggle.onValueChanged.AddListener(delegate(bool select)
        {
            luaFunction.Call(gameObject, select);
        });      
    }

    /// <summary>
    /// 添加Slider注册事件
    /// </summary>
    /// <param name="gameObject"></param>
    /// <param name="luaFunction"></param>
    public static void AddSliderOnChangeEvent(GameObject gameObject,LuaFunction luaFunction)
    {
        if(gameObject == null || luaFunction == null) return;

        Slider slider = gameObject.GetComponent<Slider>();
        if(slider == null) return;

        slider.onValueChanged.AddListener(delegate (float val)
        {
            luaFunction.Call(val);
        });
    }

    /// <summary>
    /// 清除所有Button注册事件
    /// </summary>
    /// <param name="gameObject"></param>
    public static void ClearButtonClick(GameObject gameObject)
    {
        if(gameObject == null) return;

        Button button = gameObject.GetComponent<Button>();
        if(button == null) return;

        button.onClick.RemoveAllListeners();
    }
}
